import http from '../../../common/httpUtil';
import * as types from './pictureMutationTypes';

const actions = {


  queryPicture({commit}, {pageIndex = 1, pageSize = 10} = {}) {
    commit(types.SYS_FILE_UPLOAD_QUERY_FILES_BEGIN);

    return http.getRaw(`sys/organ/list_info?page=${pageIndex}&size=${pageSize}`)
      .then(res => {
        let files = res.body.objs;
        for (var i = 0; i < files.length; i++) {
          let timedata = new Date(parseInt(files[i].CREATE_DATE)).toLocaleDateString()
          files[i].CREATE_DATE = timedata
          if (files[i].STATE === 0) {
            files[i].STATE = '已上传'
          } else if (files[i].STATE === 1) {
            files[i].STATE = '审核通过'
          } else if (files[i].STATE === 2) {
            files[i].STATE = '审核未通过'
          } else {
            files[i].STATE = ''
          }
        }
        commit(types.SYS_FILE_UPLOAD_QUERY_FILES_SUC, {files});
      });
  },

  searchFile({commit}, {fileName, pageIndex = 1, pageSize = 10} = {}) {
    commit(types.SYS_FILE_UPLOAD_QUERY_FILES_BEGIN);

    return http.getRaw(`sys/organ/list_info_search?page=${pageIndex}&size=${pageSize}&content=${fileName}`).then(res => {
      let files = res.body.objs;
      console.log(files)
      for (var i = 0; i < files.length; i++) {
        console.log(files[i].STATE)
        let timedata = new Date(parseInt(files[i].CREATE_DATE)).toLocaleDateString()
        files[i].CREATE_DATE = timedata
        if (files[i].STATE === 0) {
          files[i].STATE = '已上传'
        } else if (files[i].STATE === 1) {
          files[i].STATE = '审核通过'
        } else if (files[i].STATE === 2) {
          files[i].STATE = '审核未通过'
        } else {
          files[i].STATE = ''
        }
      }
      commit(types.SYS_FILE_UPLOAD_QUERY_FILES_SUC, {files});
    })
  },


  queryPictureCount({commit}, {pageIndex = 1, pageSize = 10} = {}) {
    commit(types.SYS_FILE_UPLOAD_QUERY_FILES_COUNT_BEGIN);

    return http.getRaw(`sys/organ/list_info?page=${pageIndex}&size=${pageSize}`)
      .then(res => {
        let totalCount = res.body.totalNum;
        commit(types.SYS_FILE_UPLOAD_QUERY_FILES_COUNT_SUC, {totalCount})
      });
  },

  searchFileCount({commit}, {fileName, pageIndex = 1, pageSize = 10} = {}) {
    commit(types.SYS_FILE_UPLOAD_QUERY_FILES_COUNT_BEGIN);

    return http.getRaw(`sys/organ/list_info_search?page=${pageIndex}&size=${pageSize}&content=${fileName}`).then(res => {
      let totalCount = res.body.totalNum;
      commit(types.SYS_FILE_UPLOAD_QUERY_FILES_COUNT_SUC, {totalCount})
    })
  },

  editFile({commit}, {id, description} = {}) {
    return http.post(`sys/organ/save_file?id=${id}&description=${description}`).then(res => {
      console.log(res)
    })
  },

  searchPicturelist({commit}, {id, callback} = {}) {
    commit(types.SYS_FILE_UPLOAD_QUERY_PICTURE_LIST_BEGIN);

    return http.post(`sys/organ/get_organ_files?userId=${id}`).then(res => {
      if (res) {
        console.log(res)
        let pictures = res;
        commit(types.SYS_FILE_UPLOAD_QUERY_PICTURE_LIST_SUC, {pictures})
      }
      callback()
    })


  }

};


export default actions;
